package cn.myfirefly.moming.portal.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.portal.dao.FunctionActionDao;
import cn.myfirefly.moming.portal.entity.MMFunctionAction;
import cn.myfirefly.moming.portal.model.FunctionActionDto;
import cn.myfirefly.moming.portal.model.FunctionActionFilterDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class FunctionActionService {
//	private Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	private FunctionActionDao functionActionDao;
	
	public PageResultDto<FunctionActionDto> getList(FunctionActionFilterDto dto, Integer funSn) {
		
		return functionActionDao.getList(dto, funSn);
	}
	
	public String save(FunctionActionDto dto) {
		MMFunctionAction funAction = null;
		// 更新
		if (StrUtil.notBlank(dto.getFunActionSn())) {
			funAction = functionActionDao.get(dto.getFunActionSn());
			if (funAction == null) {
				return "功能动作不存在，请刷新之后重试。";
			}
		} else {
		// 新增
			funAction = new MMFunctionAction();
			funAction.setCreateBy(dto.getModifyBy());
			funAction.setCreateDate(dto.getModifyDate());
		}
		BeanUtil.copyBeans(dto, funAction);
		functionActionDao.save(funAction);
		return Const.ResultCode.SUCCESS;
	}
	
	public FunctionActionDto getFunctionAction(Integer funActionSn) {
		MMFunctionAction funAction = functionActionDao.get(funActionSn);
		return BeanUtil.copyBeans(FunctionActionDto.class, funAction);
	}
	
	public String del(Integer funActionSn) {
		MMFunctionAction funAction = functionActionDao.get(funActionSn);
		if (funAction == null) {
			return "功能动作不存在，请刷新之后重试。";
		}
		if (funAction != null && !funAction.getFunActionSn().equals(funActionSn)) {
			return "功能动作已变更，请刷新之后重试。";
		}
		
		functionActionDao.del(funAction);
		return Const.ResultCode.SUCCESS;
	}
	
}
